package SIFT;

import OpenCV.cv;
import OpenCV.cv.IplImage;
import SIFT.util.*;
import SURF.Save_Interest_Points_To_File;
import com.sun.jna.Pointer;

import ij.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Extract landmark correspondences in two images as PointRoi.
 * 
 * The plugin uses the Scale Invariant Feature Transform (SIFT) by David Lowe
 * \cite{Lowe04} and the Random Sample Consensus (RANSAC) by Fishler and Bolles
 * \citet{FischlerB81} with respect to a transformation model to identify
 * landmark correspondences.
 * 
 * BibTeX:
 * <pre>
 * &#64;article{Lowe04,
 *   author    = {David G. Lowe},
 *   title     = {Distinctive Image Features from Scale-Invariant Keypoints},
 *   journal   = {International Journal of Computer Vision},
 *   year      = {2004},
 *   volume    = {60},
 *   number    = {2},
 *   pages     = {91--110},
 * }
 * &#64;article{FischlerB81,
 *	 author    = {Martin A. Fischler and Robert C. Bolles},
 *   title     = {Random sample consensus: a paradigm for model fitting with applications to image analysis and automated cartography},
 *   journal   = {Communications of the ACM},
 *   volume    = {24},
 *   number    = {6},
 *   year      = {1981},
 *   pages     = {381--395},
 *   publisher = {ACM Press},
 *   address   = {New York, NY, USA},
 *   issn      = {0001-0782},
 *   doi       = {http://doi.acm.org/10.1145/358669.358692},
 * }
 * </pre>
 * 
 * @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
 * @version 0.4b
 */
public class SIFT_ExtractPointRoi 
{	
    private static List< Feature > fs1 = new ArrayList< Feature >();
    synchronized public static List<Feature> getLastResult() {return fs1;}

    static private class Param
    {
            final public FloatArray2DSIFT.Param sift = new FloatArray2DSIFT.Param();

            /**
             * Closest/next closest neighbour distance ratio
             */
            public float rod = 0.92f;

            /**
             * Maximal allowed alignment error in px
             */
            public float maxEpsilon = 25.0f;

            /**
             * Inlier/candidates ratio
             */
            public float minInlierRatio = 0.05f;

            /**
             * Implemeted transformation models for choice
             */
            final static public String[] modelStrings = new String[]{ "Translation", "Rigid", "Similarity", "Affine" };
            public int modelIndex = 0;
    }

    final static private Param p = new Param();


    public static void run(FloatArray2D _imp1)
    {

            fs1.clear();
            FloatArray2DSIFT sift = new FloatArray2DSIFT( p.sift );
            SIFT ijSIFT = new SIFT( sift );
            long start_time = System.currentTimeMillis();
            ijSIFT.extractFeatures( _imp1, fs1 );
           
    }

    public static void main(String argv[])
    {

      cv.loadHighgui();
      cv.loadCv();
      cv.loadCxcore();

      FloatArray2D imp1;
      Pointer img = cv.LoadImage("Joints1.bmp", 0);
      IplImage labD = new IplImage(img);
      byte[] bfl = HGR.Core.Pointer2Byte(img);
      float[] _imp1 = new float[bfl.length];
      for(int k=0; k<bfl.length; k++){
        if(bfl[k]<0)
            _imp1[k] = bfl[k] & 0xFF;
        else
            _imp1[k] = bfl[k];
      }



      //SIFT_ExtractPointRoi hh = new SIFT_ExtractPointRoi(_imp1, labD.width, labD.height);
      imp1 = new FloatArray2D(_imp1, labD.width, labD.height);
      SIFT_ExtractPointRoi.run(imp1);

      Save_Interest_Points_To_File FilePoints = new Save_Interest_Points_To_File();
        FilePoints.SiftFile(0,"xx");
    }
}
